//
//  ByHourDatePicker.m
//  Homept_ios
//
//  Created by pro on 16/10/17.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByHourDatePicker.h"

@interface ByHourDatePicker () <UIPickerViewDelegate>

@property (nonatomic, strong) UIPickerView *picker;

@property (nonatomic, strong) NSMutableArray    *yearItems;             // 年
@property (nonatomic, strong) NSMutableArray    *monthItems;            // 月
@property (nonatomic, strong) NSMutableArray    *dayItems;              // 日
@property (nonatomic, strong) NSArray           *hourItems;             // 时
@property (nonatomic, strong) NSArray           *minuteItems;           // 分

@property (nonatomic, strong) NSArray           *titleItems;            //

@property (nonatomic, assign) NSInteger         yearIndex;
@property (nonatomic, assign) NSInteger         monthIndex;
@property (nonatomic, assign) NSInteger         dayIndex;
@property (nonatomic, assign) NSInteger         hourIndex;
@property (nonatomic, assign) NSInteger         minuteIndex;

@end

@implementation ByHourDatePicker



- (instancetype)init {
    
    if (self = [super init]) {
        
        _yearIndex = 0;
        _monthIndex = 0;
        _dayIndex = 0;
        _hourIndex = 0;
        _monthIndex = 0;
        self.usePickerTitleForCustomContentMode = YES;
        [self initUI];
        
        __weak typeof(self) weakself = self;
        self.customTapOk = ^ {
            if (weakself.dateDidPicker) {
                NSString *year = weakself.yearItems[weakself.yearIndex];
                NSString *month = weakself.monthItems[weakself.monthIndex];
                NSString *day = weakself.dayItems[weakself.dayIndex];
                NSString *hour = weakself.hourItems[weakself.hourIndex];
                NSString *minute = weakself.minuteItems[weakself.minuteIndex];

                weakself.dateDidPicker(@[year, month, day, hour, minute]);
            }
        };
    }
    
    return self;
}


- (void)initUI {
    
    _picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 216)];
    _picker.delegate = self;
    self.contentView = _picker;
}

#pragma mark - picker delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return self.titleItems.count * 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component % 2 == 0) {       // 奇数列
        NSInteger index = component / 2;
        switch (index) {
            case 0:
                return self.yearItems.count;
                break;
            case 1:
                return self.monthItems.count;
                break;
            case 2:
                return self.dayItems.count;
                break;
            case 3:
                return self.hourItems.count;
                break;
            case 4:
                return self.minuteItems.count;
                break;
            default:
                break;
        }
    }
    else {                          // 偶数列，显示，年，月，日字样
        return 1;
    }
    return 1;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
    return 35;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    if (component % 2 == 0) {
        return pickerView.width * 0.85 * (2.0 / 3) / _titleItems.count;
    } else {
        return pickerView.width * 0.85 / 3 / _titleItems.count;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {

    if (component % 2 == 0) {       // 奇数列
        NSInteger index = component / 2;
        if (index == 0) {           // 选择 年
            _yearIndex = row;
            _monthIndex = 0;
            _dayIndex = 0;
        }
        if (index == 1) {           // 选择 月
            _monthIndex = row;
            _dayIndex = 0;
            // 重置天数数组
            NSInteger year = [self.yearItems[_yearIndex] integerValue];
            NSInteger month = [self.monthItems[_monthIndex] integerValue];
            self.dayItems = [self getDays:year month:month];
        }
        if (index == 2) {           // 选择 日
            _dayIndex = row;
        }
        if (index == 3) {           // 选择 时
            _hourIndex = row;
        }
        if (index == 4) {           // 选择 时
            _minuteIndex = row;
        }
        [pickerView reloadAllComponents];
        [pickerView selectRow:_yearIndex inComponent:0 animated:YES];
        [pickerView selectRow:_monthIndex inComponent:2 animated:YES];
        [pickerView selectRow:_dayIndex inComponent:4 animated:YES];
        [pickerView selectRow:_hourIndex inComponent:6 animated:YES];
        [pickerView selectRow:_minuteIndex inComponent:8 animated:YES];
    }
    else {                          // 偶数列
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view {
    
    UILabel *label = [[UILabel alloc] init];
    label.textColor = self.itemColor;
    label.font = FONT(14);
    
    if (component % 2 == 0) {       // 奇数列
        label.textAlignment = NSTextAlignmentCenter;

        NSInteger index = component / 2;
        switch (index) {
            case 0:
                label.text = self.yearItems[row];
                break;
            case 1:
                label.text = self.monthItems[row];
                break;
            case 2:
                label.text = self.dayItems[row];
                break;
            case 3:
                label.text = self.hourItems[row];
                break;
            case 4:
                label.text = self.minuteItems[row];
                break;
            default:
                break;
        }
    }
    else {                          // 偶数列，显示，年，月，日字样
        label.textAlignment = NSTextAlignmentLeft;
        NSInteger index = component / 2;
        label.text = self.titleItems[index];
        label.userInteractionEnabled = YES;
    }

   
    return label;
}


#pragma mark - getter
- (NSArray *)titleItems {
    if (!_titleItems) {
        _titleItems = @[@"年", @"月", @"日", @"时", @"分"];
    }
    return _titleItems;
}
- (NSMutableArray *)yearItems {
    if (!_yearItems) {
        
        _yearItems = [self getYears];
    }
    return _yearItems;
}
- (NSMutableArray *)monthItems {
    if (!_monthItems) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int i = 1; i <= 12; i ++) {
            [temp addObject:[NSString stringWithFormat:@"%i", i]];
        }
        _monthItems = temp;
    }
    return _monthItems;
}
- (NSMutableArray *)dayItems {
    if (!_dayItems) {
        NSInteger year = [self.yearItems[_yearIndex] integerValue];
        NSInteger month = [self.monthItems[_monthIndex] integerValue];
        _dayItems = [self getDays:year month:month];
    }
    return _dayItems;
}
- (NSArray *)hourItems {
    if (!_hourItems) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int i = 0; i < 24; i ++) {
            [temp addObject:[NSString stringWithFormat:@"%i", i]];
        }
        _hourItems = [temp copy];
    }
    return _hourItems;
}
- (NSArray *)minuteItems {
    if (!_minuteItems) {
        NSMutableArray *temp = [NSMutableArray array];
        for (int i = 0; i < 60; i ++) {
            [temp addObject:[NSString stringWithFormat:@"%i", i]];
        }
        _minuteItems = [temp copy];
    }
    return _minuteItems;
}

#pragma mark - fuzhu 
- (NSMutableArray *)getYears {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]];
    NSMutableArray *temp = [NSMutableArray array];
    for (NSInteger i = components.year; i < components.year + 20; i ++) {
        [temp addObject:[NSString stringWithFormat:@"%li", i]];
    }
    
    return temp;
}

- (NSMutableArray *)getDays:(NSInteger)year month:(NSInteger)month {
    NSLog(@"%li,%li", year, month);
    NSInteger day = 0;
    if((month == 1) || (month == 3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12))
    {
        day = 31 ;
    }
    if((month == 4) || (month == 6) || (month == 9) || (month == 11))
    {
        day = 30;
    }
    if (month == 2) {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        {
            // 闰年
            day = 29;
        }
        else
        {
            // 平年
            day = 28;
        }
    }
    
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    for (NSInteger i = 1; i <= day; i ++) {
        [temp addObject:[NSString stringWithFormat:@"%li", i]];
    }
    
    return temp;
}





@end
